/**
 * Created by oneminute on 7/16/14.
 */
var assert = require("assert");
var should = require("should");
var logger = require("../lib/utils/logger");
var testutils = require("./testutils");
var lz = require("../lib/utils/modules")();
var httpMocks = require("./lib/http-mock");
var mockExpress = require("./lib/mockexpress");
var route = require("../routes/route");
var userFilter = require("../routes/userfilter");
var login = require("../routes/login");
var register = require("../routes/register");

describe("测试route模块。", function () {

    var app = mockExpress();

    it("正常运行route模块。", function (done) {
        route(app);
        done();
    });

    describe("测试UserFilter子模块。", function () {
        var tmpUser = null;
        var tmpUser2 = null;

        it("成功调用UserFilter，不传入uid。", function (done) {
            var request = httpMocks.createExpressRequest({
                method: 'GET',
                url: '/search'
            });
            var response = httpMocks.createExpressResponse();
            var next = function (_req, _res) {
                testutils.check(done, function () {
                    assert.notEqual(_req.user, null);
                    assert.equal(_req.user.isTemp(), true);
                    tmpUser = _req.user;
                });
            };
            next = next.bind(undefined, request, response);
            userFilter(request, response, next);
        });

        it("成功调用UserFilter，传入已存在的uid。", function (done) {
            var request = httpMocks.createExpressRequest({
                method: 'GET',
                url: '/search?uid=' + tmpUser.id
            });
            var response = httpMocks.createExpressResponse();
            var next = function (_req, _res) {
                testutils.check(done, function () {
                    assert.notEqual(_req.user, null);
                    assert.equal(_req.user.isTemp(), true);
                    assert.equal(_req.user.id, tmpUser.id);
                });
            };
            next = next.bind(undefined, request, response);
            userFilter(request, response, next);
        });

        it("成功调用userfilter，传入不存在的uid。", function (done) {
            var request = httpMocks.createExpressRequest({
                method: 'get',
                url: '/search?uid=t_000000'
            });
            var response = httpMocks.createExpressResponse();
            var next = function (_req, _res) {
                testutils.check(done, function () {
                    assert.notEqual(_req.user, null);
                    assert.equal(_req.user.isTemp(), true);
                    assert.equal(_req.user.id, _req.query.uid);
                    tmpUser2 = _req.user;
                });
            };
            next = next.bind(undefined, request, response);
            userFilter(request, response, next);
        });

        after(function (done) {
            lz.User.deleteByAId(tmpUser2._id, function () {
                lz.User.deleteByAId(tmpUser._id, function() {
                    done();
                });
            });
        });
    });

    describe("单独测试/register请求。", function () {
        var tmpUser = null;
        var normalUser = null;
        var normalUser2 = null;

        before(function (done) {
            lz.User.createNew(function (_err, _tmpuser) {
                tmpUser = _tmpuser;
                done();
            });
        });

        it("成功请求/register，使用临时用户发送请求。", function (done) {
            var request = httpMocks.createExpressRequest({
                method: 'post',
                url: '/register',
                body: {
                    name: "test0001",
                    password: "test0001",
                    email: "test0001@lz.com"
                }
            });
            var response = httpMocks.createExpressResponse();
            register.post(request, response, {user: tmpUser}, function (_err, _user, _args) {
                lz.ArangoDB.getObjectById(tmpUser._id, function (_tmpErr, _tmpUser) {
                    testutils.check(done, function () {
                        assert.equal(_tmpUser, null);
                        assert.equal(_err, null);
                        normalUser = _user;
                    });
                });
            });
        });

        it("成功请求/register，使用普通用户发送请求。", function (done) {
            var request = httpMocks.createExpressRequest({
                method: 'post',
                url: '/register',
                body: {
                    name: "test0002",
                    password: "test0002",
                    email: "test0001@lz.com"
                }
            });
            var response = httpMocks.createExpressResponse();
            register.post(request, response, {user: normalUser}, function (_err, _user, _args) {
                testutils.check(done, function () {
                    assert.equal(_err, null);
                    normalUser2 = _user;
                });
            });
        });

        it("请求/register,用户名已存在。", function (done) {
            var request = httpMocks.createExpressRequest({
                method: 'post',
                url: '/register',
                body: {
                    name: "test0001",
                    password: "test0001",
                    email: "test0001@lz.com"
                }
            });
            var response = httpMocks.createExpressResponse();
            register.post(request, response, {user: normalUser}, function (_err, _user, _args) {
                testutils.check(done, function () {
                    _err.code.should.equal(lz.Config.errors.REGISTER_USER_EXISTED.code);
                });
            });
        });

        after(function (done) {
            lz.User.deleteByAId(normalUser._id, function () {
                lz.User.deleteByAId(normalUser2._id, function() {
                    done();
                });
            });
        });
    });

    describe("单独测试/login请求。", function () {
        var tmpUser = null;
        var normalUser = null;
        before(function (done) {
            lz.User.createNew(function (_err, _tmpuser) {
                tmpUser = _tmpuser;
                lz.User.createNew({
                        id: lz.Utils.getUUID("u_"),
                        name: "test0001",
                        password: "test0001"},
                    function (_err, _normaluser) {
                        normalUser = _normaluser;
                        done();
                    });
            });
        });

        it("成功请求/login，使用临时用户作为当前用户。", function (done) {
            var request = httpMocks.createExpressRequest({
                method: 'post',
                url: '/login',
                body: {
                    name: "test0001",
                    password: "test0001"
                }
            });
            var response = httpMocks.createExpressResponse();
            login.post(request, response, {user: tmpUser}, function (_err, _user, _args) {
                lz.ArangoDB.getObjectById(tmpUser._id, function (_tmpErr, _tmpUser) {
                    testutils.check(done, function () {
                        assert.equal(_tmpUser, null);
                        assert.equal(_err, null);
                        assert.equal(_user, _args);
                    });
                });
            });
        });

        it("成功请求/login，使用普通用户作为当前用户。", function (done) {
            var request = httpMocks.createExpressRequest({
                method: 'post',
                url: '/login',
                body: {
                    name: "test0001",
                    password: "test0001"
                }
            });
            var response = httpMocks.createExpressResponse();
            login.post(request, response, {user: normalUser}, function (_err, _user, _args) {
                testutils.check(done, function () {
                    assert.equal(_err, null);
                    assert.equal(_user, _args);
                });
            });
        });

        it("请求/login，但用户不存在。", function (done) {
            var request = httpMocks.createExpressRequest({
                method: 'post',
                url: '/login',
                body: {
                    name: "test0001xxxxxxxxx",
                    password: "test0001"
                }
            });
            var response = httpMocks.createExpressResponse();
            login.post(request, response, {user: normalUser}, function (_err, _user, _args) {
                testutils.check(done, function () {
                    _err.code.should.equal(lz.Config.errors.LOGIN_USER_NOT_FOUND.code);
                });
            });
        });

        it("请求/login，但密码错误。", function (done) {
            var request = httpMocks.createExpressRequest({
                method: 'post',
                url: '/login',
                body: {
                    name: "test0001",
                    password: "test0001xxxxxx"
                }
            });
            var response = httpMocks.createExpressResponse();
            login.post(request, response, {user: normalUser}, function (_err, _user, _args) {
                testutils.check(done, function () {
                    _err.code.should.equal(lz.Config.errors.LOGIN_WRONG_PASSWORD.code);
                });
            });
        });

        after(function (done) {
            lz.User.deleteByAId(normalUser._id, function () {
                done();
            });
        });
    });
});